package com.xiliu.server.modules.app.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiliu.common.result.R;
import com.xiliu.common.utils.StringUtils;
import com.xiliu.common.utils.ValidateUtil;
import com.xiliu.server.common.utils.SecurityUtils;
import com.xiliu.server.modules.app.vo.BecomeVo;
import com.xiliu.server.modules.become.entity.XlBecome;
import com.xiliu.server.modules.become.entity.XlBecomeItem;
import com.xiliu.server.modules.become.service.IXlBecomeItemService;
import com.xiliu.server.modules.become.service.IXlBecomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author xiliu
 * @date 2022/12/30 11:47
 */
@Api(tags = "移动端我的相关")
@RestController
@RequestMapping("/mobile/user")
public class UserController {

    @Autowired
    private IXlBecomeService becomeService;
    @Autowired
    private IXlBecomeItemService becomeItemService;

    @ApiOperation("检查用户是否有申请房管员认证")
    @ApiImplicitParam(name = "userId",value = "用户id", required = true,paramType = "query")
    @GetMapping("auth")
    public R checkAuth(@RequestParam Long userId) {
        long count = becomeService.count(new LambdaQueryWrapper<XlBecome>()
                .eq(XlBecome::getUserid,userId)
                .eq(XlBecome::getState,1));
        return R.ok(count);
    }

    @ApiOperation("用户认证提交")
    @PostMapping("auth-add")
    public R authAdd(@Valid @RequestBody BecomeVo vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return R.validate(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        if (vo.getLevel() == 3) {
            boolean checkIdnum = ValidateUtil.validateIdnum(vo.getIdnum());
            if (!checkIdnum) {
                return R.validate("身份证号格式错误");
            }
            if (CollectionUtil.isEmpty(vo.getImgs())) {
                return R.validate("身份证照片不能为空");
            }
        }
        Long userid = SecurityUtils.getLoginUser().getUser().getUserId();
        vo.setUserid(userid);
        boolean result = becomeService.creatBecome(vo);
        if (result) {
            return R.ok("提交成功，请等待审核");
        }
        return R.error();
    }

    @ApiOperation("查看申请认证")
    @ApiImplicitParam(name = "userId",value = "用户id", required = true,paramType = "query")
    @GetMapping("auth-show")
    public R authShow(@RequestParam Long userId) {
        BecomeVo vo = new BecomeVo();
        XlBecome xlBecome = becomeService.getOne(new LambdaQueryWrapper<XlBecome>()
                .eq(XlBecome::getUserid,userId).orderByDesc(XlBecome::getCreatedTime),false);
        if (xlBecome == null) {
            return R.error("认证不存在");
        }
        BeanUtils.copyProperties(xlBecome, vo);
        vo.setDate(DateUtil.format(xlBecome.getBecomeDate(),"yyyy-MM-dd HH:mm:ss"));
        if (StringUtils.isNotBlank(xlBecome.getImg())) {
            vo.setImgUrl(Arrays.asList(xlBecome.getImg().split(",")));
        }
        if (vo.getLevel() == 2) {
            List<XlBecomeItem> items = becomeItemService.list(new LambdaQueryWrapper<XlBecomeItem>().eq(XlBecomeItem::getBecomeId,xlBecome.getBecomeId()));
            if (CollectionUtil.isNotEmpty(items)) {
                for (XlBecomeItem item : items) {
                    item.setImgUrl(Arrays.asList(item.getImg().split(",")));
                }
            }
            vo.setHouse(items);
        }
        return R.ok(vo);
    }
}
